<?php
	class Gate extends Controller{
		function Gate(){
			parent::Controller();
		}
		/*
		 * Function index is loaded by default when you load PollWatcher site.
		 */
		function index(){
			$data=array();
			if($this->session->userdata('logged_in')){
				$id = $this->session->userdata('id');
 				redirect('user');
			}
			else{
				$gate['body'] = $this->load->view('gate/home', $data, TRUE);
				$gate['login'] = $this->load->view('gate/login', $data, TRUE);
				$this->load->view('home', $gate);
			}		
		}

		/*
		 * Destroys a session, and a user would be logged out.
		 */
		function logout() {
			$this->session->sess_destroy();
			redirect('gate');
		}		
		
		/*
		 * Registration page.
		 * Basically, this is where a validation upon registration is done.
		 */
		function register(){	
			$data = array();
			$rules = array();
			$rules['email'] = "required|valid_email|callback_user_exists";
			$rules['email_conf'] = "required|valid_email|matches[email]";
			$rules['password'] = "required|alpha_numeric|min_length[6]";	
			$rules['password_conf'] = "required|alpha_numeric|min_length[6]|matches[password]";
			$rules['firstname'] = "required";
			$rules['lastname'] = "required";
			$rules['gender'] = "required";
			$rules['month'] = "required|callback_valid_date";
			$rules['day'] = "required";
			$rules['year'] = "required";

			$this->validation->set_rules($rules);
			$this->validation->set_error_delimiters('<div id="error">', '</div>');
			$fields = array();
			$fields['email'] = 'email';
			$fields['email_conf'] = 'email confirmation';
			$fields['password'] = 'password';			
			$fields['password_conf'] = 'password confirmation';
			$fields['firstname'] = 'first name';
			$fields['lastname'] = 'last name';
			$fields['middlename'] = 'middle name';
			$fields['gender'] = 'gender';
			$fields['month'] = 'month';
			$fields['day'] = 'day';
			$fields['year'] = 'year';
			
			$this->validation->set_fields($fields);
			if ($this->validation->run()==TRUE){
				$new['email'] = $this->input->post('email');
				$salt = random_string('alnum', 40);
				$new['salt'] = $salt;
				$new['activation_code'] = sha1(sha1($this->input->post('email')));
				$password = $this->input->post('password');
				$hash = sha1($salt.$password);
				$new['password'] = $hash;
				$new['firstname'] = $this->input->post('firstname');
				$new['lastname'] = $this->input->post('lastname');
				$new['middlename']= $this->input->post('middlename');
				$new['gender']= $this->input->post('gender');
				$new['month'] = $this->input->post('month');
				$new['day'] = $this->input->post('day');
				$new['year'] = $this->input->post('year');

				$month = $this->input->post('month'); 
				$day = $this->input->post('day');
				$year = $this->input->post('year');
				$months = array('jan'=>1,'feb'=>2,'mar'=>3,'april'=>4,'may'=>5,'june'=>6,'july'=>7,'august'=>8,'sept'=>9,'oct'=>10,'nov'=>11,'dec'=>12); 
				$new['birthday']= date("Y-m-d", mktime(0,0,0,$months[$month],$day,$year));
				$new['reg_date'] = date("Y-n-j");
				$this->Register->create_user($new);
				$config = Array(
				  'protocol' => 'smtp',
				  'smtp_host' => 'ssl://smtp.googlemail.com',
				  'smtp_port' => 465,
				  'smtp_user' => 'pollwatcher.god@gmail.com',
				  'smtp_pass' => 'ichigokurakurasakiU',
				);
				$this->load->library('email', $config);
				$this->email->set_newline("\r\n");

				$this->email->from('pollwatcher.god@gmail.com', 'PollWatcher');
				$this->email->to($this->input->post('email'));

				$this->email->subject('Welcome to PollWatcher!');
				
				$msg = $this->load->view('register', $new, TRUE);
				$this->email->message($msg);
				
				if (!$this->email->send())
				  show_error($this->email->print_debugger());
				
				$this->session->set_flashdata('message', 'Please check your e-mail to confirm your registration.');
				
				redirect('gate');			
			}
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/register', $data, TRUE);
			$this->load->view('home', $gate);
			
		}

		/*
		 * Loads features views.
		 */
		function features(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/features', $data, TRUE);
			$this->load->view('home', $gate);
		}

		/*
		 * Loads Help & Support Views 
		 */
		function help_support(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/help_support', $data, TRUE);
			$this->load->view('home', $gate);
		}

		/*
		 * Loads About Us Views
		 */
		function about_us(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$gate['body'] = $this->load->view('gate/about_us', $data, TRUE);
			$this->load->view('home', $gate);
		}

		/*
		 * Loads login screen in the sidebar.
		 * Authentication is also done here.
		 */
		function login(){
			$data = array();
			$gate['login'] = $this->load->view('gate/login', $data, TRUE);
			$this->load->view('gate/login', $gate);
			$password = $this->input->post('password');
			$email = $this->input->post('username');
			$salt = $this->Login->getSalt($email);
			$user = $this->Login->authenticate('user', $email, $password, $salt);

			if($user == FALSE){
				if($this->Login->isPending('user', $email) == true)
				{
					$this->session->set_flashdata('message', 'Please check your e-mail to confirm your registration.');
				}
				else
				{

					$this->session->set_flashdata('message', 'Error. Please try again');
				}
			}
			else{
				$user = $this->Login->fetch('user', $email, $password, $salt);
				$sessionData = array(
					'firstname' => $user['firstname'],
					'lastname' => $user['lastname'],
					'gender' => $user['gender'],
					'email' => $user['email'],
					'id' => $user['user_id'],
					'logged_in' => TRUE,
					'status'=>$user['status'],
					'reg_date'=>$user['reg_date'],
					'last_login'=>$user['last_login'],
				);
				$this->session->set_userdata($sessionData);
			}
			$this->Login->last_login('user', $email);
			redirect('gate');			
		}
		
		/*
		 * Activation of account is done here.
		 */
		function activate()
		{
			$activation_code = $this->uri->segment(3);
			$this->Register->activate($activation_code);
			echo "<script>javascript:alert('Account successfully activated! You are now able to login.'); window.location = 'http://localhost/pollwatcher/index.php/gate'</script>";
		}
		
		function valid_date()
		{
			if ($this->input->post('month') == "Month:" || $this->input->post('day') == "Day:" || $this->input->post('year') == "Year:")
			{
				$this->validation->set_message('valid_date', 'The Birthday field is invalid.');
				return FALSE;
			}
			
			return TRUE;
		}
		
		function user_exists()
		{
			if($this->Register->exists($this->input->post('email')))
			{
				$this->validation->set_message('user_exists', 'Email already exists.');
				return FALSE;
			}
			return TRUE;
		}
		
	}
?>
